package day04_zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

import java.io.IOException;

public class TestCRUD {
    public static final String connectString = "master:2181," + "slave1:2181," + "slave2:2181";
    public static final int sessionTimeout = 2000;

    @Test
    public void testConnect() {
        ZooKeeper zk = null;
        try {
            zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {//连接成功也会回调
                    System.out.println("===" + watchedEvent.getType() + "=====" + watchedEvent.getState());
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (true){
            if(zk.getState() == ZooKeeper.States.CONNECTED) {
                System.out.println("连接状态==" + zk.getState());
                break;
            }
        }
    }

}
